﻿/*----------------------------------------------EXTEND FRAMEWORK----------------------------------------------*/
(function($) {
    $.extend({
        /// <summary>Create new jquery lement</summary>
        /// <param name="attributes">list of attribute</param>
        /// <param name="children">list of children</param>
        /// <returns>New jquery element</returns>
        create: function(element, attributes, children) {
            var elem = $(document.createElement(element));
            if (typeof (attributes) == 'object') {
                for (key in attributes) {
                    elem.attr(key, attributes[key]);
                }
            }
            if (typeof (children) == 'string') {
                elem.text(children);
            } else if (typeof (children) == 'object') {
                for (i = 0; i < children.length; i++) {
                    elem.append(children[i]);
                }
            } return elem;
        },
        /// <summary>Check if use IE8 and emulate IE7</summary>
        /// <returns>Indicates if emulate IE7 into IE8</returns>
        MsIeEmulate: function() {
            var isEmulate7 = false;
            $('meta').each(function() {
                var $meta = $(this);
                if ($meta.attr('httpEquiv').toLowerCase() == 'X-UA-Compatible'.toLowerCase()) {
                    if ($meta.attr('content').toLowerCase() == 'IE=EmulateIE7'.toLowerCase()) {
                        isEmulate7 = true;
                        return;
                    }
                }
            });
            return isEmulate7;
        },
        /// <summary>Get size of page</summary>
        /// <returns>
        ///     Height : height of page
        ///     Width : width of page            
        /// </returns>
        getPageSize: function() {
            return { Height: $(window).height(), Width: $(document).width() };
        }
    });
})(jQuery);

/*-----------------------------------------------EXTEND CONTROL-----------------------------------------------*/
var m_dimensions = new Array();
var m_water = new Array();

jQuery.fn.extend({
    /// <summary>Set Absolute position to control</summary>
    /// <param name="position">
    ///     Top : top position
    ///     Left: left position
    /// </param>
    /// <returns>jQuery element</returns>
    setAbsolutePosition: function(position) {
        $(this).css('position', 'absolute').css('top', position.Top).css('left', position.Left);
        return $(this);
    },
    /// <summary>Calculate center position</summary>
    /// <returns>Center position</returns>
    calcCenterPosition: function() {
        var pageSize = $.getPageSize();
        var w = (pageSize.Width - $(this).innerWidth()) / 2;
        var h = (pageSize.Height - $(this).innerHeight()) / 2;
        return { Top: h, Left: w };
    },
    /// <summary>Maximized control</summary>
    /// <returns>jQuery element</returns>
    maximized: function() {
        var pg = $.getPageSize();
        m_dimensions[$(this).attr('id')] = { width: $(this).width(), height: $(this).height() };
        $(this).width(pg.Width).height(pg.Height).setAbsolutePosition({ Top: 0, Left: 0 });
        return $(this);
    },
    /// <summary>Normalized to original size</summary>
    /// <returns>jQuery element</returns>
    normalized: function() {
        var dim = m_dimensions[$(this).attr('id')];

        if (dim) {
            $(this).width(dim.width).height(dim.height).setAbsolutePosition($(this).calcCenterPosition());
        }

        return $(this);
    },
    /// <summary>Minimized control to icon</summary>
    /// <param name="title">Title to icon bar</param>
    /// <param name="cssClass">Class for bar</param>
    /// <returns>jQuery Element</returns>
    minimized: function(title, cssClass) {
        var $control = $(this);
        var idControl = $control.attr('id');
        var idIcon = 'icon_' + idControl;
        var $icon = $('#' + idIcon);

        // check if is modal
        var mt = idControl.match('content_');
        var $modal;

        if (mt != null) {
            $modal = $('#modal_' + idControl.substring(mt.lastIndex, idControl.length));

            if ($modal.length != 0) {
                $modal.css('display', 'none');
            }
        }

        if ($icon.length == 0) {
            $icon = $.create('div', { id: idIcon }).dblclick(function() {
                $(this).css('display', 'none');
                $control.css('display', '');

                if ($modal.lenght != 0) {
                    $modal.css('display', '');
                }
            }).addClass(cssClass).setAbsolutePosition({ Top: 0, Left: 0 }).html(title);

            $('body').append($icon);
        }
        $(this).css('display', 'none');
        $icon.css('display', '');

        return $control;
    },
    /// <summary>Create WaterMark for control</summary>
    /// <param name="text">text for waterMark effect</param>
    /// <param name="cssClass">class for control</param>
    /// <returns>jQuery element</returns>
    createWaterMark: function(text, cssClass) {
        var $control = $(this);
        var id = $control.attr('id');

        if (m_water[id] == undefined) {
            m_water[$control.attr('id')] = { Text: text, CssClass: cssClass };
            $control.addClass(cssClass).val(text);

            $control.bind('blur', null, function(e) {
                if ($control.val() == '') {
                    $control.addClass(cssClass).val(text);
                }
            }).bind('focus', null, function(e) {
                if ($control.val() == text) {
                    $control.removeClass(cssClass).val('');
                }
            });
        }

        return $control;
    },
    /// <summary>Create confirm function</summary>
    /// <param name="attributes">
    ///     questionText: Text for question
    ///     clientSuccess: client function where client click 'OK'
    ///     clientCancel: client function where client click 'Cancel'
    ///     submitClient: indicates if execute old client function
    ///     submitServer: indicates if execute server function
    /// </param>
    /// <returns>jQuery element</returns>
    createConfirm: function(attributes) {
        var $control = $(this);
        var oldAction = $control.attr('onclick');

        $control.attr('onclick', null).click(function(e) {
            var clientResult = confirm(attributes.questionText);

            if (clientResult) {
                if (attributes.clientSuccess) {
                    attributes.clientSuccess();
                }
            } else {
                if (attributes.clientCancel) {
                    attributes.clientCancel();
                }

                return false;
            }

            if (attributes.submitClient && oldAction != null) {
                oldAction();
            }

            return attributes.submitServer;
        });

        return $control;
    },
    /// <summary>Insert function to first position on event</summary>
    /// <param name="eventName">Name of attribute where write function</param>
    /// <param name="f">New first function</param>
    /// <param name="useResult">Indicates if check result previous execute old action</param>
    /// <returns>jQuery Element</returns>
    first: function(eventName, f, useResult) {
        var $control = $(this);
        var oldAction = $control.attr(eventName);

        $control.attr(eventName, null).click(function(e) {
            var result = f();

            if (useResult) {
                if (!result) {
                    return false;
                }
            }

            if (oldAction != null) {
                oldAction();
            }
        });

        return $control;
    },
    /// <summary>Gets or sets current position of cursor into control</summary>
    /// <param name="begin">Start position</param>
    /// <param name="end">End position</param>
    /// <returns>
    ///     begin: current start position
    ///     end: current end position
    /// </returns>
    caret: function(begin, end) {
        if (this.length == 0) return;
        if (typeof begin == 'number') {
            end = (typeof end == 'number') ? end : begin;
            return this.each(function() {
                if (this.setSelectionRange) {
                    this.focus();
                    this.setSelectionRange(begin, end);
                } else if (this.createTextRange) {
                    var range = this.createTextRange();
                    range.collapse(true);
                    range.moveEnd('character', end);
                    range.moveStart('character', begin);
                    range.select();
                }
            });
        } else {
            if (this[0].setSelectionRange) {
                begin = this[0].selectionStart;
                end = this[0].selectionEnd;
            } else if (document.selection && document.selection.createRange) {
                var range = document.selection.createRange();
                begin = 0 - range.duplicate().moveStart('character', -100000);
                end = begin + range.text.length;
            }
            return { begin: begin, end: end };
        }
    }
});